package com.me.forum.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.me.forum.dao.IUserDao;
import com.me.forum.domain.User;
import com.me.forum.request.UserRequest;
import com.me.forum.service.IUserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
public class UserService extends BaseService<IUserDao,User> implements IUserService {

    /**
	*@description:用户注册服务接口实现
	*@author:LWP
	*@createTime:2020-04-26 19:43
	*/
	@Override
	public User register(UserRequest ureq) {
		if (!ureq.isLegal()) {
			return null;
		}
		
		User user = dao.findUserByUsername(ureq.getUsername());
		if (user != null) {
			return null;
		}
		
		int saveNum = dao.register(ureq);
		return saveNum == 1? dao.findUserByUsername(ureq.getUsername()) : null;
	}

	/**
	*@description:登录
	*@author:ly
	*@createTime:2020-05-06 10:28
	*/
	@Override
	public User login(String username, String password) {
		return dao.findUserByUsernameAndPassword(username,password);
	}

	@Override
	public boolean
	updateById(UserRequest ureq) {
		if (!ureq.isLegal()) {
			return false;
		}
		return dao.updateById(ureq) == 1;
	}

    @Override
    public IPage<User> getUsers(UserRequest ureq, String page, String size) {
        return dao.selectByAll(new Page<User>(Long.valueOf(page),Long.valueOf(size)),ureq);
    }

	@Override
	@Transactional
	public boolean addUser(UserRequest ureq) {
		if (!ureq.isLegal()) {
			return false;
		}
		User user = dao.findUserByUsername(ureq.getUsername());
		if (user != null) {
			return false;
		}
 		int save = dao.insertSelective(ureq);

		if (ureq.getCategoryIds() != null && ureq.getCategoryIds().length >= 1) {
			dao.insertByRole(ureq);
		}
		return save == 1;
	}
}




















